package com.klhappy.houyuan;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.baidu.mapapi.SDKInitializer;
import com.klhappy.houyuan.utils.ConfigManager;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.squareup.okhttp.OkHttpClient;

import android.app.Application;
import android.content.Context;

public class HouYuanApplication extends Application {

    private static OkHttpClient mHttpClient = new OkHttpClient();
    private RequestQueue mVolleyQueue = null;
    private ConfigManager mConfigManage = null;

    @Override
    public void onCreate() {
        super.onCreate();
        SDKInitializer.initialize(getApplicationContext());
        mVolleyQueue = Volley.newRequestQueue(this);
        mConfigManage = new ConfigManager(this);

        initImageLoader(getApplicationContext());
    }

    public static OkHttpClient getHttpClientInstance() {
        return mHttpClient;
    }

    public RequestQueue getVolleyRequest() {
        return mVolleyQueue;
    }

    public ConfigManager getConfigManager() {
        return mConfigManage;
    }

    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(
                context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.writeDebugLogs(); // Remove for release app

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config.build());
    }
}
